/*******************************************************************************
 * Copyright 2011 Alexandre Zglav and Morphiastic
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.morphiastic.analysis.filters;

import java.io.IOException;
import java.util.Map;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import com.morphiastic.analysis.AnalysisComponentDefinition;
import com.morphiastic.utils.EqualsUtil;
public class ShingleTokenFilter extends ElasticFilter {

	
	public static final String MAX_SHINGLE_SIZE_PARAM_KEY = "max_shingle_size";
	public static final String OUTPUT_UNIGRAMS_PARAM_KEY = "output_unigrams";
	
	
	private Integer maxShingleSize;
	private Boolean outputUnigrams;
	
	
	public ShingleTokenFilter(String defName){
		this(null,defName);
	}
	
	
	public ShingleTokenFilter(Map<String,String> params, String defName){
		
		super(ElasticFilterType.shingle, defName);
		
		if(params!=null){
			String maxShinglStr = params.get(MAX_SHINGLE_SIZE_PARAM_KEY);
			if(maxShinglStr!=null && !maxShinglStr.isEmpty()){
				try {
					int max = Integer.parseInt(maxShinglStr);
					this.maxShingleSize = max;
				} catch (NumberFormatException e) {
				}
			}
		
			String outputStr = params.get(MAX_SHINGLE_SIZE_PARAM_KEY);
			if(outputStr!=null && !outputStr.isEmpty()){
				this.outputUnigrams = Boolean.parseBoolean(outputStr);
			}
		}
	}
	
	

	@Override
	public boolean hasCustomParams() {
		 if(this.maxShingleSize!= null || this.outputUnigrams!=null )return true;
		 return false;
	}
	
	
	
	@Override
	public XContentBuilder addToBuilder(
			XContentBuilder mappingBuilder) throws IOException {

		mappingBuilder.startObject(getDefinitionName())
		.field("type", getComponentSubTypeName());
		
		if(this.maxShingleSize != null){
			mappingBuilder.field(MAX_SHINGLE_SIZE_PARAM_KEY, this.maxShingleSize.intValue());
		}
		if(this.outputUnigrams != null){
			mappingBuilder.field(OUTPUT_UNIGRAMS_PARAM_KEY, this.outputUnigrams.booleanValue());
		}
		return mappingBuilder;
	}
	
	
	@Override
	public void setParamsFromSettings(Settings tokenizerSettings) {
		
		String maxSettings = tokenizerSettings.get(MAX_SHINGLE_SIZE_PARAM_KEY);
		if(maxSettings!=null && !maxSettings.isEmpty()){
			try {
				int mtl = Integer.parseInt(maxSettings);
				this.maxShingleSize = mtl;
			} catch (NumberFormatException e) {
			}
		}
		
		String outputSettings = tokenizerSettings.get(OUTPUT_UNIGRAMS_PARAM_KEY);
		this.outputUnigrams = Boolean.parseBoolean(outputSettings);
	}
	
	@Override
	public boolean areEquivalentDefinitions(AnalysisComponentDefinition otherTok){
		
		if(otherTok==null)return false;
		ShingleTokenFilter ot ;
		try {
			ot = (ShingleTokenFilter)otherTok;
		} catch (ClassCastException e) {
			return false;
		}
		return 
		EqualsUtil.areEqual(this.maxShingleSize, ot.maxShingleSize)
		&&
		EqualsUtil.areEqual(this.outputUnigrams, ot.outputUnigrams);
	}
		
	
	
}
